//
//  HotRecommendedController.m
//  迷笛
//
//  Created by . on 2018/9/7.
//  Copyright © 2018年 🍃🌺🌺🍃. All rights reserved.
//

#import "MIDIHotRecommendedController.h"
#import "MIDIHotRcommendedCell.h"
#import "MIDIactivitymodel.h"
#import "MIDIactivityWebView.h"
@interface MIDIHotRecommendedController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView * Hottable;


@property(nonatomic,strong)NSMutableArray *activityArr;

@property(nonatomic,copy)NSDictionary * dic;



@property(nonatomic,copy)NSMutableDictionary * listDictionary;

@property(nonatomic,strong)NSString * activity;



@end

@implementation MIDIHotRecommendedController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:@selector(Block:)];
    self.navigationItem.leftBarButtonItem = leftBarItem;
    
    [self.navigationItem.leftBarButtonItem setImage:[[UIImage imageNamed:@"后退"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    _activityArr = [NSMutableArray array];
    
    [self Activityrequest];

    [self HomeUI];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return _activityArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString * cellID = @"cell";
    MIDIHotRcommendedCell * cell =[tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell)
    {
        cell = [[MIDIHotRcommendedCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
        cell.backgroundColor = [UIColor colorWithHexString:@"#1B212F"];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;

    }
    MIDIactivitymodel *mm1 = _activityArr[indexPath.row];
 
    cell.maninIamge.image =[UIImage imageNamed:@"IMG_0195.jpg"];
    
    [cell.maninIamge sd_setImageWithURL:[NSURL URLWithString:mm1.activityCover] placeholderImage:nil];

    cell.address.text = mm1.activityName;
    cell.day.text = @"倒计时:3天";
    cell.coordinates.text = mm1.activityCity;
    NSString * timeStr;

    timeStr = [NSString stringWithFormat:@"%@-%@",mm1.statred_at,mm1.ended_at];

    cell.time.text = timeStr;
    return cell;
}
-(void)Activityrequest
{
    NSString * homeActivity = @"activity";
    _activity = [NSString stringWithFormat:@"%@%@",MIDIApiRoot,homeActivity];

    [EdHttp get:_activity params:nil progress:^(id downloadProgress)
    {
        
    }
        success:^(id responseObject)
    {
        NSLog(@"活动接口%@",responseObject);
        NSArray *arr4 = responseObject[@"data"];
        
        for (NSDictionary *dic4 in arr4)
        {
            NSString * activityname;
            NSString * activitycity;
            NSString * statred_at;
            NSString * ended_at;
            NSString * activitycover;
            NSNumber * activitID;
            if (![dic4[@"name"] isKindOfClass:[NSNull class]])
            {
                activityname = dic4[@"name"];
            }
            else
            {
                activityname = @"";
            }
            if (![dic4[@"city"] isKindOfClass:[NSNull class]])
            {
                activitycity = dic4[@"city"];
            }
            else
            {
                activitycity = @"";
            }
            if (![dic4[@"statred_at"] isKindOfClass:[NSNull class]])
            {
                statred_at = dic4[@"statred_at"];
            }
            else
            {
                statred_at = @"";
            }
            if (![dic4[@"ended_at"] isKindOfClass:[NSNull class]])
            {
                ended_at = dic4[@"ended_at"];
            }
            else
            {
                ended_at = @"";
            }
            if (![dic4[@"cover"] isKindOfClass:[NSNull class]])
            {
                activitycover = dic4[@"cover"];
            }
            else
            {
                activitycover = @"";
            }
            if (![dic4[@"id"] isKindOfClass:[NSNull class]])
            {
                activitID = dic4[@"id"];
            }
            else
            {
//                activitID = @"";
            }

       
            MIDIactivitymodel * mmact = [MIDIactivitymodel new];
            mmact.activityName = activityname;
            mmact.activityCity =activitycity;
            mmact.statred_at = statred_at;
            mmact.ended_at = ended_at;
            mmact.activityCover = activitycover;
            mmact.ID = activitID;
            [self->_activityArr addObject:mmact];
        
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self->_Hottable reloadData];
            
        });
//        ---------------
//        _dic = responseObject;
//        self.activityArr = _dic[@"data"];
//        NSLog(@"接口接口%@",self.activityArr);
//        for (int i = 0; i<[self.activityArr count]; i++)
//        {
//            self.listDictionary = [self.activityArr objectAtIndex:i];
//            _activityName = self.listDictionary[@"name"];
//            NSLog(@"DIC%@",_activityName);
//
//            MIDIactivitymodel * actvitymodel = [MIDIactivitymodel new];
//            actvitymodel.activityName =name;
//            NSLog(@"activity网络请求%@",self.activityName);
//
//            [self->_activityArr addObject:actvitymodel];
//        }
//        -------------
    }
        failure:^(NSError *error)
    {
        NSLog(@"失败");
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kDevice_Is_iPhoneX?HeighX(236):HeightScale(236);
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
{
    return nil;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
{
    return nil;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MIDIactivityWebView * midiWeb =[[MIDIactivityWebView alloc]init];
    MIDIactivitymodel *mm1 = _activityArr[indexPath.row];
    midiWeb.ID = mm1.ID;
    midiWeb.name = mm1.activityName;
    [self.navigationController pushViewController:midiWeb animated:YES];
}
//视图将要出现时（导航颜色字体颜色）
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.translucent = NO;//Bar的模糊效果，默认为YES
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"#171717"];
    self.title = @"热门推荐";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#FFFFFF"],NSFontAttributeName : [UIFont fontWithName:@"Helvetica-Bold" size:WidthScale(17)]}];
    //    去掉导航黑线
    UIImage *img = [UIImage imageNamed:@""];
    [self.navigationController.navigationBar setBackgroundImage:img  forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
}
// 返回上一级
-(void)Block:(UIButton * )sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)HomeUI
{
    [self.view addSubview:self.Hottable];
}
-(UITableView *)Hottable
{
    if (!_Hottable)
    {
        _Hottable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, kDevice_Is_iPhoneX?HeighX(730):HeightScale(600)) style:UITableViewStyleGrouped];
        _Hottable.dataSource = self;
        _Hottable.delegate = self;
        _Hottable.backgroundColor = [UIColor colorWithHexString:@"#1B212F"];
        _Hottable.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _Hottable;
}
@end
